package com;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author wdl
 */
@WebServlet(name = "CarServlet")
public class CarServlet extends HttpServlet {
    private static final String INSERT_OPERATION = "insert";
    private static final String HOME_PAGE_QUERY_OPERATION = "homeQuery";
    private static final String UPDATE_FREE_TIME_OPERATION = "updateFreeTime";
    private static final String USER_CHECK_QUERY_OPERATION = "userCheckQuery";
    private static final String CHECK_QUERY_OPERATION = "checkQuery";
    private static final String UPDATE_CHECK_STATE_OPERATION = "updateCheckState";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //charset=utf-8使得可以显示中文
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        //从客户端获取数据
        String operation = request.getParameter("operation");

        //获得数据库操作对象
        CarDB cardb = new CarDB();
        //结果返回客户端
        JSONObject object = new JSONObject();

        List<Car> carList;
        switch (operation) {
            //插入操作
            case INSERT_OPERATION: {
                String account = request.getParameter("account");
                String carNumber = request.getParameter("carNumber");
                String carBand = request.getParameter("carBand");
                String image = request.getParameter("image");
                String freeTime = request.getParameter("freeTime");
                int state = Integer.parseInt(request.getParameter("state"));
                int checkState = Integer.parseInt(request.getParameter("checkState"));

                //创建实体类
                System.out.println(account + carNumber + carBand + image + freeTime + state + checkState);
                Car car = new Car(account, null, carNumber, carBand, image, freeTime, state, checkState);
                //插入数据
                int result = cardb.insertCar(car);
                object.put("result", result);
                break;
            }
            //首页可预约车辆搜索
            case HOME_PAGE_QUERY_OPERATION: {
                carList = cardb.homeQueryCarData();
                JSONArray data = new JSONArray();
                JSONObject obj;
                for (Car car :
                        carList) {
                    obj = new JSONObject();
                    obj.put("account", car.getAccount());
                    obj.put("name", car.getName());
                    obj.put("carNumber", car.getCarNumber());
                    obj.put("carBand", car.getCarBand());
                    obj.put("image", car.getImage());
                    obj.put("freeTime", car.getFreeTime());
                    obj.put("state", 0);
                    obj.put("checkState", 0);
                    data.add(obj);
                }
                object.put("data", data);
                break;
            }
            //更新空闲时间
            case UPDATE_FREE_TIME_OPERATION: {
                String carNumber = request.getParameter("carNumber");
                String newFreeTime = request.getParameter("newFreeTime");
                //更新空闲时间
                int result = cardb.updateCarFreeTime(carNumber, newFreeTime);
                object.put("result", result);
                break;
            }
            //用户查看审核进度
            case USER_CHECK_QUERY_OPERATION: {
                String account = request.getParameter("account");
                carList = cardb.queryCarByAccount(account);
                JSONArray data = new JSONArray();
                for (Car car : carList) {
                    JSONObject obj = new JSONObject();
                    obj.put("account", account);
                    obj.put("name", car.getName());
                    obj.put("carNumber", car.getCarNumber());
                    obj.put("carBand", car.getCarBand());
                    obj.put("image", car.getImage());
                    obj.put("freeTime", car.getFreeTime());
                    obj.put("state", car.getState());
                    obj.put("checkState", car.getCheck());
                    data.add(obj);
                }
                object.put("userCheck", data);
                break;
            }
            //管理员审核
            case CHECK_QUERY_OPERATION: {
                int check = Integer.parseInt(request.getParameter("checkState"));
                carList = cardb.queryNoCheckCarList(check);
                JSONArray data = new JSONArray();
                for (Car car : carList) {
                    JSONObject obj = new JSONObject();
                    obj.put("account", car.getAccount());
                    obj.put("name", car.getName());
                    obj.put("carNumber", car.getCarNumber());
                    obj.put("carBand", car.getCarBand());
                    obj.put("image", car.getImage());
                    obj.put("freeTime", car.getFreeTime());
                    obj.put("state", car.getState());
                    obj.put("checkState", check);
                    data.add(obj);
                }
                object.put("managerCheck", data);
                break;
            }

            //审核更新
            case UPDATE_CHECK_STATE_OPERATION: {
                String carNumber = request.getParameter("carNumber");
                int result = CarDB.updatacheckcar(carNumber);
                object.put("updateResult", result);
                break;
            }
            default:
        }

        out.print(object);
        out.flush();
        out.close();
    }
}
